local ClientOHOH = _G.ClientOHOH;

local M = ClientOHOH.class.new(script, ClientOHOH.files.BaseObject())
local CallBackManager = ClientOHOH.load.CallBackManager();
local Good_Cfg=ClientOHOH.config.Goods
local ConnectClass=_G.CommonOHOH.load.Connect();
local PetTool=_G.CommonOHOH.load.PetTool();
ClientOHOH.load.PetCell()
local buyData={
	gid = 501	
}
local instStr={
	cell = "ctrl.petList.petCopy",
	petList = "ctrl.petList.peListtFrame",
	voidTime = "ctrl.content.time",

	craftBtn ="ctrl.content.btn",
	content="ctrl.content.contentFrame" ,
	copy = "ctrl.slotlist.copy",

	viewportFrame= "ctrl.slotlist.ViewportFrame"
}

function M.new(...)
	return M(...);
end;

function M.ctor(self,ctrl)
	M.super.ctor(self);
	self.ctrl=ctrl
	self.slotList = {}
	self.ui=ctrl:InitUiInst2(ctrl.main_inst,instStr)

	self.ctrl.UpdateSlotListEvent:Connect(function(startTime,voidTime,petId)
		self:UpdateSlotList(startTime,voidTime,petId)
	end)
	self.ctrl.ResetSlotEvent:Connect(function(slotId)
		self:ResetSlot(slotId)
	end)
	self.ctrl.BuyDoneEvent:Connect(function(slotId)
		self:BuyDoneUpdateSlot(slotId)
	end)
	self.ctrl.SCaleTimeUpdateSlotListEvent:Connect(function(voidtime)
		self:ScaleTimeUpdateSlotList(voidtime)
	end)
	self.ctrl.DatabaseReadyEvent:Connect(function()
        self.database = self.ctrl:GetDataBase()
		-- local data=self.database:GetData("pets")
		-- local t = self.ctrl:GetVoidTime()
		-- self:VoidingDataInit(data,t)
    end)
	self.ctrl.VoidTimeGetEvent:Connect(function(voidtime)
		local data=self.database:GetData("pets")
		local t = self.ctrl:GetVoidTime()
		self:VoidingDataInit(data,t)
    end)
	--进度---改造SlotList，将PetVoidGui部分迁移到SlotList，下一步将Slot构造
	self:Init()		
end;

function M.Init(self)
	self:CopySlotCells(6)
end

function M.CopySlotCells(self, numCopies)

	for i = 1, numCopies do
		self.slotList[i]=self:CreateSlot(i)
	end

end
function M.CreateSlot(self,id)

	local slot = require(script.Parent.Slot).new(id,self.ui.copy.Parent)
	slot.MakeClickEvent:Connect(function()
		self.ctrl:MakeClick()
	end)
	slot.BuyClickEvent:Connect(function()
		buyData.gType = Good_Cfg[buyData.gid].gType
		buyData.petId = slot:GetPetId()
		buyData.slotId = slot.orderId
		--print("#### slot.petId",buyData)
		self.ctrl.BuyEvent:Action(buyData)
	end)
	slot.ReceiveClickEvent:Connect(function()
		self.ctrl.ReceiveEvent:Action(slot.startTime,slot.orderId,slot.hadBuy)
	end)
	return slot

end
function M.UpdateSlotList(self,startTime,voidTime,petId)
	for id, slotCopy in pairs(self.slotList) do
		local isEmpty = slotCopy.main_inst:GetAttribute("Empty")
		if isEmpty then
			local Model=PetTool.GetPetModelByPetId(petId)
			Model:SetPrimaryPartCFrame(slotCopy.main_inst.ViewportFrame:WaitForChild("CF").Value)
			Model.Parent=slotCopy.main_inst.ViewportFrame
			Model.Parent.Visible  =true
			Model.Name  ="Model"
			slotCopy.main_inst:SetAttribute("Empty", false)
			slotCopy.main_inst.idle.Visible = false
			slotCopy.main_inst.idleBtn.Visible = false
			slotCopy.main_inst.progress.Visible = true
			slotCopy.main_inst.progressBtn.Visible = true
			slotCopy.needTime =voidTime
			slotCopy.startTime =startTime
			slotCopy.petId =  petId
			slotCopy.main_inst.progress.time.Text =  self.ctrl.voidTimeToHHMMSS(voidTime)
			slotCopy:StartUpdateTime()
			break
		end
	end
end
function M.ResetSlot(self,slotId)
	for _, slotCopy in pairs(self.slotList) do
		local isEmpty = slotCopy.main_inst:GetAttribute("Empty")
		if not isEmpty and slotId == slotCopy.orderId then
			slotCopy.main_inst.ViewportFrame.Model:Destroy()

			slotCopy.main_inst:SetAttribute("Empty", true)
			--slotCopy.main_inst.progress.Visible = false
			--slotCopy.main_inst.progressBtn.Visible = false
			slotCopy.main_inst.receive.Visible = false
			slotCopy.main_inst.receiveBtn.Visible =  false
			slotCopy.main_inst.idle.Visible = true
			slotCopy.main_inst.idleBtn.Visible = true
			slotCopy.hadBuy = false
			slotCopy.needTime =0
			slotCopy.startTime =0
			slotCopy.petId =  nil
			slotCopy.main_inst.progress.time.Text =  self.ctrl.voidTimeToHHMMSS(0)
			--slotCopy:EndUpdateTime()
			break
		end
	end

end
function M.BuyDoneUpdateSlot(self,slotId)
	for _, slotCopy in pairs(self.slotList) do
		local isEmpty = slotCopy.main_inst:GetAttribute("Empty")
		if not isEmpty and slotId == slotCopy.orderId then
			slotCopy.hadBuy = true
			slotCopy.main_inst.progress.Visible = false
			slotCopy.main_inst.progressBtn.Visible = false
			slotCopy.main_inst.receive.Visible = true
			slotCopy.main_inst.receiveBtn.Visible =  true
			if slotCopy.update_connect then
				slotCopy.update_connect:Disconnect();
				slotCopy.update_connect=nil;
			end
			break
		end
	end

end
function M.ScaleTimeUpdateSlotList(self, voidtime)
	--print("##### self.slotList",self.slotList)
	for id, slotCopy in pairs(self.slotList) do
		local isEmpty = slotCopy.main_inst:GetAttribute("Empty")
		--print("##### self.slotCopy",slotCopy)
		if not isEmpty and slotCopy.update_connect then
			--print("##### self.slotCopy inside",slotCopy)
			slotCopy.needTime = voidtime- (os.time()-slotCopy.startTime )
		end
	end
end
function M.VoidingDataInit(self,data,tVoidTime)
	for startTime,petId in pairs(data.voiding) do
		local needtime =tVoidTime -(os.time()-startTime)
		self:UpdateSlotList(startTime,needtime,petId)
	end
end

--function M.Update(self,dt)
--	local min=math.huge
--	local count=0
--	local totalCount=0;--已经领取和可领取的数量
--	for k,v in pairs(self.OnlineData) do
--		if v:IsCanGet() then
--			self.insts.content[k].time.Text=LanguageHelper:GetText(10403)
--			self.insts.content[k].btn.ImageColor3=self.insts.content[k].colorNow.Value;
--			count=count+1
--			totalCount=totalCount+1;
--		elseif v:IsClaimed() then
--			self.insts.content[k].time.Text=LanguageHelper:GetText(10404)
--			self.insts.content[k].btn.ImageColor3=self.insts.content[k].colorOld.Value;
--			totalCount=totalCount+1;
--		else
--			if  v.time>0 then
--				v.time=v.time-1
--				self.insts.content[k].time.Text=GeneralTool.secondsToHHMM(v.time)
--				self.insts.content[k].btn.ImageColor3=self.insts.content[k].colorWill.Value;           
--			end
--			if min>v.time then
--				min=v.time
--			end
--		end
--	end
--	self.MinTimeAction:Action(count,GeneralTool.secondsToHHMM(min))
--	if totalCount== #self.OnlineData then
--		-- self.updateConnect:Disconnect();
--		-- self.updateConnect=nil;
--		-- print(self.updateConnect,"结束")
--		if self.update_connect then
--			self.update_connect:Disconnect() 
--			self.update_connect=nil
--		end
--		self.ClaimFreeAction:Action();
--	end
--end

--function M.StartUpdateTime(self)
--	if self.update_connect then
--		return 
--	end

--	self.update_connect=self:AddStepUpdate(self.Update,1)
--end


function M.dtor(self)
	M.super.dtor(self);
end;


--function M.CreateSlot(self)
--	local slot=  require(script.Parent.Slot).new()

--	return slot
--end


---------------------------- 公共函数----------------------------------------------


return M